<?php
error_reporting(E_WARNING);
include_once '../includes/metodos.php';
connectSql();
checkUser();

if(!empty($_POST['cmd']) && $_POST['cmd']== 'insert'){
	$query = mysql_query("INSERT INTO tb_agenda VALUES (NULL, ".$_POST['podo'].", NULL, '".$_POST['cliente']."', '".$_POST['data']."', '".$_POST['hora'].":00', 1, '".$_POST['tel']."', '".$_POST['cel']."')");
	if($query){
		echo '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">location.href="index.php";</SCRIPT>';
	}else{
		die(mysql_error());
	}
}
include_once '../header.php';
?>
<body> 
<!-- Start: page-top-outer -->
<div id="page-top-outer">    
	<?php include_once '../menu.php';?>

<div id="page-heading"><h1>Agenda</h1></div>


<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
<tr>
	<th rowspan="3" class="sized"><img src="<?php echo EXTERNAL_ROOT_PORTAL;?>/css/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
	<th class="topleft"></th>
	<td id="tbl-border-top">&nbsp;</td>
	<th class="topright"></th>
	<th rowspan="3" class="sized"><img src="<?php echo EXTERNAL_ROOT_PORTAL;?>/css/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
</tr>
<tr>
	<td id="tbl-border-left"></td>
	<td>
	<!--  start content-table-inner -->
	<div id="content-table-inner">
	
	<table border="0" width="100%" cellpadding="0" cellspacing="0">
	<tr valign="top">
	<td>
	
		<!--  start step-holder -->	
		<div id="step-holder">
			<div class="step-no">1</div>
			<div class="step-dark-left"><a href="">Vizualizar</a></div>
			<div class="step-dark-right">&nbsp;</div>
			<div class="step-no-off">2</div>
			<div class="step-light-left">Marcar</div>
			<div class="step-light-round">&nbsp;</div>
			<div class="clear"></div>
		</div>
	</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0">
		
		<form action="index.php" method="post">
		<label>Agenda de </label>
		<SELECT name="podo">
			<?php 
			$sql = mysql_query("SELECT * FROM tb_funcionario WHERE perfil != 4");
			while ($row = mysql_fetch_array($sql)){
				if($_POST['podo'] == $row['id']){
					echo '<OPTION SELECTED value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</OPTION>';
				}else{
					echo '<OPTION value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</OPTION>';
				}
			}
			?>
		</SELECT>
		<input type="hidden" name="cmd" value="agenda">
		<label>Data:</label><input type="text" id="datepicker" name="data">
		<input class="button" type="submit" value="Buscar">
		</form>
 </table><hr>
 
<?php 
	if(isset($_POST['cmd']) || $_POST['cmd']=='agenda'){
				//formulario para marcar agenda
				$c = 1;
				$query = mysql_query("SELECT * FROM tb_funcionario WHERE id=".$_POST['podo']);
				$row = mysql_fetch_array($query);
				$funcionario = $row['nome'].' '.$row['sobrenome'];
				if($row['id']==1){
					$hora = array(1=>'08:00',2=>'08:40', 3=>'09:20',4=>'10:00',5=>'10:40', 6=>'11:20',7=>'12:00', 8=>'12:40', 9=>'13:20', 10=>'14:00', 11=>'14:40'
								, 12=>'15:20', 13=>'16:00', 14=>'16:40', 15=>'17:20', 16=>'18:00', 17=>'18:40', 18=>'19:20');
					$cont = 19;
				}else{
					$hora = array(1=>'08:00',2=>'09:00', 3=>'10:00',4=>'11:00',5=>'12:00', 6=>'13:00',7=>'14:00', 8=>'15:00', 9=>'16:00', 10=>'17:00', 11=>'18:00', 12=>'19:00');
					$cont = 13;
				}
				$hora_encx = array(1=>'08:00',2=>'08:30', 3=>'09:30',4=>'09:30',5=>'10:00', 6=>'10:30',7=>'11:00', 8=>'11:30', 9=>'12:00', 10=>'12:30', 11=>'13:00'
								, 12=>'13:30', 13=>'14:00', 14=>'14:30', 15=>'15:00', 16=>'15:30', 17=>'16:00', 18=>'16:30', 19=>'17:00', 20=>'17:30', 21=>'18:00', 22=>'18:30', 23=>'19:00');
				$cont_encx = 24;
				
				if(empty($_POST['data'])){
					$data=date("Y-m-d");
				}else{
					$data = $_POST['data'];
				}
				$show_data = explode("-", $data);
				echo '<h3>'.$funcionario.' Data: '.$show_data[2].'/'.$show_data[1].'/'.$show_data[0].'</h3><br/></strong>';
				//Criando os horarios
				?>
				<div style=" float:left; width: 540px; height: auto;">
				<h1>Agenda</h1>
				<table width="100%" cellspacing="0" cellpadding="0" border="0" id="product-table">
				<tbody><tr>
					<th class="table-header-repeat" width="10%"><a href="">Hora</a></th>
					<th class="table-header-repeat" width="40%"><a href="">Cliente</a></th>
					<th class="table-header-repeat" width="22%"><a href="">A&ccedil;&atilde;o</a></th>
				</tr>
				<?php 
				while ($c<$cont){
					echo '<tr>';
						echo '<tr><td width="50px"><strong>'.$hora[$c].'</strong></td>';
						$agenda = mysql_query("SELECT * FROM tb_agenda WHERE hora='".$hora[$c].":00' AND data='".$data."' AND id_funcionario=".$_POST['podo']." AND (status=1 OR status=4) ORDER BY hora LIMIT 1");
						$num = mysql_num_rows($agenda);
						if($num>0){
							$row = mysql_fetch_array($agenda);
							if($row['status']==1){
								echo '<td width="320px"><label>'.$row['nm_cliente'].' '.$row['telefone'].' / '.$row['celular'].'</label></td>';
								echo '<td><form action="edit.php" method="post">
								<select name="cmd">
									<option value="faltou">Faltou</option>
									<option value="desmarcar">Desmarcar</option>
									<option value="editar">Editar</option>
									<option value="fim">Conclu&iacute;do</option>
								</select>
								<input type="hidden" name="id" value="'.$row['id'].'"/>
								<input class="button" type="submit" value="Executar"/>
								</form></td></tr>';	
							}else{
								echo '<td width="320px"><label class="texto" style="color: blue;">'.utf8_encode($row['nm_cliente']).' '.$row['telefone'].' / '.$row['celular'].'</label></td><td>Concluido</td></tr>';
							}
						}else{
							echo '<td style="color: red; width: 100px"> Vago</td>';
							echo '<td><form action="new.php" method="post">
										<input type="hidden" name="hora" value="'.$hora[$c].'"/>
										<input type="hidden" name="data" value="'.$data.'"/>
										<input type="hidden" name="podo" value="'.$_POST['podo'].'"/>
										<input class="button" type="submit" value="Marcar" />
									</form></td></tr>';
						}
						++$c;
					echo '</tr>';
				}
				?>
				</tbody></table>
				</div>
				
				<div style=" float:right; width: 540px; height: auto;">
				<h1>Encaixes</h1>
				<table width="100%" cellspacing="0" cellpadding="0" border="0" id="product-table">
				<tbody><tr>
					<th class="table-header-repeat" width="10%"><a href="">Hora</a></th>
					<th class="table-header-repeat" width="40%"><a href="">Cliente</a></th>
					<th class="table-header-repeat" width="22%"><a href="">A&ccedil;&atilde;o</a></th>
				</tr>
				<?php 
				$c=1;
				while ($c<$cont_encx){
					echo '<tr>';
						echo '<tr><td width="50px"><strong>'.$hora_encx[$c].'</strong></td>';
						$agenda = mysql_query("SELECT * FROM tb_encaixe WHERE hora='".$hora_encx[$c].":00' AND data='".$data."' AND id_funcionario=".$_POST['podo']." AND (status=1 OR status=4) ORDER BY hora LIMIT 1");
						$num = mysql_num_rows($agenda);
						if($num>0){
							$row = mysql_fetch_array($agenda);
							if($row['status']==1){
								echo '<td width="320px"><label>'.$row['nm_cliente'].' '.$row['telefone'].' / '.$row['celular'].'</label></td>';
								echo '<td><form action="encaixe_edit.php" method="post">
								<select name="cmd">
									<option value="faltou">Faltou</option>
									<option value="desmarcar">Desmarcar</option>
									<option value="editar">Editar</option>
									<option value="fim">Conclu&iacute;do</option>
								</select>
								<input type="hidden" name="id" value="'.$row['id'].'"/>
								<input class="button" type="submit" value="Executar"/>
								</form></td></tr>';	
							}else{
								echo '<td width="320px"><label class="texto" style="color: blue;">'.utf8_encode($row['nm_cliente']).' '.$row['telefone'].' / '.$row['celular'].'</label></td><td>Concluido</td></tr>';
							}
						}else{
							echo '<td style="color: red; width: 100px"> Vago</td>';
							echo '<td><form action="encaixe_new.php" method="post">
										<input type="hidden" name="hora" value="'.$hora_encx[$c].'"/>
										<input type="hidden" name="data" value="'.$data.'"/>
										<input type="hidden" name="podo" value="'.$_POST['podo'].'"/>
										<input class="button" type="submit" value="Marcar" />
									</form></td></tr>';
						}
						++$c;
					echo '</tr>';
				}
				?>
				</tbody></table>
				</div>
				<?php }	?>
 
<div class="clear"></div>
 
</div>
<!--  end content-table-inner  -->
</td>
<td id="tbl-border-right"></td>
</tr>
<tr>
	<th class="sized bottomleft"></th>
	<td id="tbl-border-bottom">&nbsp;</td>
	<th class="sized bottomright"></th>
</tr>
</table>

<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer -->

 

<div class="clear">&nbsp;</div>
    
<?php include_once '../footer.php';?>
 
</body>
</html>